<template>
	<view class="container">
		<view class="list-cell m-t" @click="toPage('set/personalData')" hover-class="cell-hover" :hover-stay-time="50">
			<view class="cell-tit-other">
				<u-avatar :src="avatar ? avatar : '/static/user/avatar.png'" size="96rpx"></u-avatar>
				<view class="cell-tit-body">
					<view class="name">{{ name }}</view>
					<!-- <view class="sex">{{ userInfo.sex == 0 ? '男' : userInfo.sex == 1 ? '女' : '' }}</view> -->
				</view>
			</view>
			<text class="cell-more iconfont icon-more1"></text>
		</view>
<!-- 		<view class="list-cell m-t" @click="toPage('set/identity')" hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tit">更换手机号</text>
			<text class="cell-other">{{ phone }}</text>
			<text class="cell-more iconfont icon-more1"></text>
		</view> -->
		<!-- <view class="list-cell b-b m-t" @click="toPage('address/address')" hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tit">地址管理</text>
			<text class="cell-more iconfont icon-more1"></text>
		</view> -->

<!-- 		<view class="list-cell m-t" hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tit">接收消息通知</text>
			<u-switch space="2" v-model="notice" asyncChange activeColor="#FF5900" inactiveColor="#E5E5E5"
				@change="changeNews"></u-switch>
		</view> -->

<!-- 		<view class="list-cell m-t" @click="clearCache" hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tit">清除缓存</text>
			<text class="cell-other">{{ fileSizeString }}</text>
			<text class="cell-more iconfont icon-more1"></text>
		</view>
		<view class="list-cell m-t" @click="toPage('public/about')" hover-class="cell-hover" :hover-stay-time="50">
      <text class="cell-tit">关于炎华潮玩</text>
      <text class="cell-more iconfont icon-more1"></text>
    </view> -->
		<view class="list-cell log-out-btn" @click="clickLogOut"><text class="cell-tit">退出登录</text></view>
		<BaseToast title="是否退出当前账号!" :show="showOut" @cancel="showOut = false" @submit="logout"></BaseToast>

		<!-- 提示弹窗 -->
		<BaseToast :show="showClose" :title="title" :content="content" @cancel="cancel" @submit="submit"></BaseToast>
	</view>
</template>

<script>
import pageMixin from '@/mixins/pageMixin';
import { mapState, mapActions,mapMutations } from 'vuex';
import BaseToast from '@/components/BaseToast/BaseToast.vue';

export default {
	mixins: [pageMixin],
	components: {
		BaseToast
	},
	data () {
		return {
			showOut: false,
			fileSizeString: null,
			notice: true,
			showClose: false,
			content: '您还没有开启通知权限，无法接受到消息通知，是否前往设置？',
			title: '通知权限开启提醒'
		};
	},
	computed: {
		...mapState('user', ['name', 'avatar', 'token', 'userInfo'])
	},
	mounted () {
		// console.log(70, uni.getStorageSync('notice'))
		// if (uni.getStorageSync('notice')) {
		// 	//存过
		// 	let obj = JSON.parse(uni.getStorageSync('notice'));
		// 	this.notice = obj.value
		// }
		// console.log(1111, this.userInfo);
		// this.formatSize();
		// this.getQuanxian(); //判断是否系统是否开启通知
	},
	methods: {
		...mapActions('user', ['Logout']),
    ...mapMutations('user', ['SET_TOKEN']),
		//退出登录
		logout () {
			//this.Logout()
			// uni.setStorageSync('Token', '')
	      	// this.SET_TOKEN('')

			this.Logout().then(() => {
				this.showOut = false;
				uni.switchTab({
					url: '/pages/appUser/user'
				});
			});
		},
		clickLogOut () {
				uni.$emit('handelConversationList');
				this.showOut = true;
		},
		// 计算缓存
		formatSize () {
			plus.cache.calculate(size => {
				let sizeCache = parseInt(size);
				if (sizeCache == 0) {
					this.fileSizeString = '0B';
				} else if (sizeCache < 1024) {
					this.fileSizeString = sizeCache + 'B';
				} else if (sizeCache < 1048576) {
					this.fileSizeString = (sizeCache / 1024).toFixed(2) + 'KB';
				} else if (sizeCache < 1073741824) {
					this.fileSizeString = (sizeCache / 1048576).toFixed(2) + 'MB';
				} else {
					this.fileSizeString = (sizeCache / 1073741824).toFixed(2) + 'GB';
				}
			});
		},
		// 清除缓存
		clearCache () {
			let os = plus.os.name;
			if (os == 'Android') {
				let main = plus.android.runtimeMainActivity();
				let sdRoot = main.getCacheDir();
				let files = plus.android.invoke(sdRoot, 'listFiles');
				let len = files.length;
				for (let i = 0; i < len; i++) {
					let filePath = '' + files[i]; // 没有找到合适的方法获取路径，这样写可以转成文件路径
					plus.io.resolveLocalFileSystemURL(
						filePath,
						entry => {
							if (entry.isDirectory) {
								entry.removeRecursively(
									entry => {
										//递归删除其下的所有文件及子目录
										uni.showToast({
											title: '缓存清理完成',
											duration: 2000
										});
										this.formatSize(); // 重新计算缓存
									},
									e => {
										console.log(e.message);
									}
								);
							} else {
								entry.remove();
							}
						},
						e => {
							console.log('文件路径读取失败');
						}
					);
				}
			} else {
				// ios
				plus.cache.clear(() => {
					uni.showToast({
						title: '缓存清理完成',
						duration: 2000
					});
					this.formatSize();
				});
			}
		},

		changeNews (e) {
			// 去判断权限
			this.getQuanxian(e, 'change');
		},
		submit () {
			//跳转去手机权限设置
			this.openTongZhi();
		},
		cancel () {
			console.log('取消,代表没有权限');
			this.showClose = false;
			// push.off(); //关闭push推送
			this.notice = false;
		},
		getQuanxian (e, type) {
			let platform = uni.getSystemInfoSync().platform; //首先判断app是安卓还是ios
			console.log(platform);
			if (platform == 'ios') {
				//这里是ios的方法
				console.log('我是iOS');
				var UIApplication = plus.ios.import('UIApplication');
				var app = UIApplication.sharedApplication();
				var enabledTypes = 0;
				if (app.currentUserNotificationSettings) {
					var settings = app.currentUserNotificationSettings();
					enabledTypes = settings.plusGetAttribute('types');
					console.log('enabledTypes1:' + enabledTypes);
					if (enabledTypes == 0) {
						//如果enabledTypes = 0 就是通知权限没有开启
						this.notice = false;
						// 没有权限
						this.showClose = true;
						uni.removeStorageSync('notice');
					} else {
						if (type) {
							// 有权限
							if (e) {
								// 开启 接收通知
								// push.on();
							} else {
								// 关闭通知
								// push.off();
							}
							this.notice = e;
							let obj = {
								name: '通知',
								value: this.notice
							};
							uni.setStorageSync('notice', JSON.stringify(obj));
						}
					}
				}
				plus.ios.deleteObject(settings);
			} else if (platform == 'android') {
				//下面是安卓的方法
				console.log('我是安卓', plus.android);
				var main = plus.android.runtimeMainActivity();
				var pkName = main.getPackageName();
				var uid = main.getApplicationInfo().plusGetAttribute('uid');
				var NotificationManagerCompat = plus.android.importClass('android.support.v4.app.NotificationManagerCompat');
				//android.support.v4升级为androidx
				if (NotificationManagerCompat == null) {
					NotificationManagerCompat = plus.android.importClass('androidx.core.app.NotificationManagerCompat');
				}
				var areNotificationsEnabled = NotificationManagerCompat.from(main).areNotificationsEnabled();
				// 未开通‘允许通知’权限，则弹窗提醒开通，并点击确认后，跳转到系统设置页面进行设置
				console.log(139, '安卓');
				if (!areNotificationsEnabled) {
					console.log(141, '安卓');
					// 没有权限
					this.notice = false;
					this.showClose = true;
					uni.removeStorageSync('notice');
				} else {
					console.log(145, '安卓有权限');
					if (type) {
						// 有权限
						if (e) {
							// 开启 接收通知
							// push.on();
						} else {
							// 关闭通知
							// push.off();
						}
						this.notice = e;
						let obj = {
							name: '通知',
							value: this.notice
						};
						uni.setStorageSync('notice', JSON.stringify(obj));
					}
				}
			}
		},
		//开启通知
		openTongZhi () {
			//弹窗按钮绑定方法
			let platform = uni.getSystemInfoSync().platform; //获取安卓还是ios
			this.tongzhi = false;
			if (platform == 'ios') {
				//如果机型是ios，ios由于权限问题，可能需要手动开启
				var UIApplication = plus.ios.import('UIApplication');
				var app = UIApplication.sharedApplication();
				var settings = app.currentUserNotificationSettings();
				enabledTypes = settings.plusGetAttribute('types');
				var NSURL2 = plus.ios.import('NSURL');
				var setting2 = NSURL2.URLWithString('app-settings:');
				var application2 = UIApplication.sharedApplication();
				application2.openURL(setting2);
				plus.ios.deleteObject(setting2);
				plus.ios.deleteObject(NSURL2);
				plus.ios.deleteObject(application2);
				plus.ios.deleteObject(settings);
			} else if (platform == 'android') {
				//如果机型是安卓
				var main = plus.android.runtimeMainActivity();
				var pkName = main.getPackageName();
				var uid = main.getApplicationInfo().plusGetAttribute('uid');
				var Intent = plus.android.importClass('android.content.Intent');
				var Build = plus.android.importClass('android.os.Build');
				//android 8.0引导
				if (Build.VERSION.SDK_INT >= 26) {
					//判断安卓系统版本
					var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
					intent.putExtra('android.provider.extra.APP_PACKAGE', pkName);
				} else if (Build.VERSION.SDK_INT >= 21) {
					//判断安卓系统版本
					//android 5.0-7.0
					var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
					intent.putExtra('app_package', pkName);
					intent.putExtra('app_uid', uid);
				} else {
					//(<21)其他--跳转到该应用管理的详情页
					intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
					var uri = Uri.fromParts('package', mainActivity.getPackageName(), null);
					intent.setData(uri);
				}
				// 跳转到该应用的系统通知设置页
				main.startActivity(intent);
			}
		}
	}
};
</script>

<style lang="scss" scoped>
page {
	background: $page-color-base;
}

::v-deep .popup .tips {
	text-align: center;
}

.list-cell {
	display: flex;
	align-items: center;
	padding: 20rpx $page-row-spacing;
	line-height: 60rpx;
	position: relative;
	background: #fff;
	justify-content: center;

	&.log-out-btn {
		position: fixed;
		bottom: 0;
		width: 100%;

		.cell-tit {
			text-align: center;
			font-size: 32rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #1a1a1a;
		}
	}

	&.cell-hover {
		background: #fafafa;
	}

	&.b-b:after {
		left: 30rpx;
	}

	&.m-t {
		margin-top: 16rpx;
	}

	.cell-more {
		align-self: center;
		font-size: 18rpx;
		margin-left: 10rpx;
	}

	.cell-other {
		color: #999999;
	}

	.cell-tit {
		flex: 1;
		margin-right: 10rpx;
		font-size: 32rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #1a1a1a;
	}

	.cell-tip {
		font-size: $font-base;
		color: $font-color-light;
	}

	.cell-tit-other {
		display: flex;
		align-items: center;
		flex: 1;
	}

	.cell-tit-body {
		margin-left: 24rpx;

		.name {
			margin-bottom: 16rpx;
			font-size: 32rpx;
			line-height: 32rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #1a1a1a;
		}

		.sex {
			font-size: 24rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #999999;
			line-height: 24rpx;
		}
	}

	switch {
		transform: translateX(16rpx) scale(0.84);
	}
}
</style>
